<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-tabs v-model="activeName">
        <el-tab-pane label="角色管理" name="first">
          <el-row style="height:60px">
            <el-button type="primary" size="small" icon="el-icon-plus" @click="addRole">添加角色</el-button>
          </el-row>
          <el-table
            :data="list"
            style="width: 100%"
          >
            <el-table-column
              label="序号"
              width="120"
              type="index"
            >
            </el-table-column>
            <el-table-column
              label="角色名称"
              width="240"
              prop="name"
            >
            </el-table-column>
            <el-table-column
              label="描述"
              prop="description"
            >
            </el-table-column>
            <el-table-column
              label="操作"
            >
              <template v-slot="{row}">
                <el-button size="small" type="success" @click="assignPerm(row.id)">分配权限</el-button>
                <el-button size="small" type="primary" @click="editRole(row.id)">编辑</el-button>
                <el-button size="small" type="danger" @click="delRole(row.id)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <el-row type="flex" justify="center">
            <el-pagination
              layout="prev, pager, next"
              :page-size="page.pagesize"
              :total="page.total"
              @current-change="handleCurrentChange"
            >
            </el-pagination>

          </el-row>
        </el-tab-pane>
        <el-tab-pane label="公司信息" name="second">
          <el-alert
            title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
            type="info"
            show-icon
            :closable="false"
          />
          <el-form label-width="120px" style="margin-top:50px">
            <el-form-item label="公司名称">
              <el-input v-model="formData.name" disabled style="width:400px" />
            </el-form-item>
            <el-form-item label="公司地址">
              <el-input v-model="formData.companyAddress" disabled style="width:400px" />
            </el-form-item>
            <el-form-item label="邮箱">
              <el-input v-model="formData.mailbox" disabled style="width:400px" />
            </el-form-item>
            <el-form-item label="备注">
              <el-input v-model="formData.remarks" type="textarea" :rows="3" disabled style="width:400px" />
            </el-form-item>
          </el-form>
        </el-tab-pane>
      </el-tabs>
    </div>
    <el-dialog v-bind="$attrs" :title="roleForm.id?'编辑角色':'新增角色'" :visible="showDialog" v-on="$listeners" @open="onOpen" @close="onClose">
      <el-form
        ref="roleFormRef"
        :model="roleForm"
        :rules="rules"
        size="medium"
        label-width="120px"
        label-position="left"
      >
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" placeholder="请输入角色名称" clearable :style="{width: '100%'}">
          </el-input>
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="roleForm.description" placeholder="请输入角色描述" clearable :style="{width: '100%'}">
          </el-input>
        </el-form-item>
      </el-form>
      <el-row slot="footer" type="flex" justify="center">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="handelConfirm">确定</el-button>
      </el-row>
    </el-dialog>

    <el-dialog title="分配权限" :visible="showPermDialog" @close="closePerm">
      <el-tree ref="treeRef" :data="permData" :props="{label: 'name'}" default-expand-all show-checkbox check-strictly node-key="id" :default-checked-keys="selectCheck"></el-tree>
      <el-row slot="footer" type="flex" justify="center">
        <el-button @click="closePerm">取消</el-button>
        <el-button type="primary" @click="handelPermConfirm">确定</el-button>
      </el-row>
    </el-dialog>

  </div>
</template>

<script>
import { addRole, assignPerm, deleteRole, getCompanyInfo, getRoleDetail, getRoleList, updateRole } from '@/api/setting.js'
import { getPermissionList } from '@/api/permission'
import { tranListToTreeData } from '@/utils'
export default {
  name: 'Setting',
  components: {},
  props: {},
  data() {
    return {
      activeName: 'first',
      list: [],
      page: {
        page: 1,
        pagesize: 2,
        total: 0
      },
      formData: {
      },
      showDialog: false,
      roleForm: {
        name: undefined,
        description: undefined
      },
      rules: {
        name: [{
          required: true,
          message: '请输入角色名称',
          trigger: 'blur'
        }],
        description: []
      },
      showPermDialog: false,
      permData: [], // 权限列表
      selectCheck: [], // 选择权限的ids
      roleId: undefined// 保存数据
    }
  },
  async created() {
    this.getRoleList()
    this.formData = await getCompanyInfo(this.$store.state.user.userInfo.companyId)
  },
  mounted() {},
  methods: {
    addRole() {
      this.showDialog = true
    },
    async editRole(id) {
      // 数据回显
      this.roleForm = await getRoleDetail(id)
      this.showDialog = true
    },
    onOpen() {},
    onClose() {
      this.close()
    },
    close() {
      // this.roleForm = {
      //   name: undefined,
      //   description: undefined
      // }
      this.roleForm = this.$options.data().roleForm
      this.$refs['roleFormRef'].resetFields()
      this.showDialog = false
    },
    handelConfirm() {
      // 表单校验
      this.$refs['roleFormRef'].validate(async valid => {
        if (!valid) return
        // 发请求
        if (this.roleForm.id) {
          // 编辑
          await updateRole(this.roleForm)
        } else {
          // 新增
          await addRole(this.roleForm)
        }
        this.$message.success('操作成功')
        // 刷新
        this.getRoleList()
        this.close()
      })
    },
    async delRole(id) {
      try {
        // 提示
        await this.$confirm('是否删除?')
        // 点击是
        // 发请求
        await deleteRole(id)
        // 提示成功
        this.$message.success('删除成功')
        // 刷新列表
        this.getRoleList()
      } catch (e) {
        // 点击否
      }
    },
    handleCurrentChange(val) {
      this.page.page = val
      this.getRoleList()
    },
    async getRoleList() {
      const { total, rows } = await getRoleList(this.page)
      this.page.total = total
      this.list = rows
    },
    async assignPerm(id) {
      this.roleId = id// 保存当前行的角色id
      this.permData = tranListToTreeData(await getPermissionList(), '0')
      const { permIds } = await getRoleDetail(id)
      this.selectCheck = permIds.map(t => t instanceof Object ? t.id : t)
      this.showPermDialog = true
    },
    closePerm() {
      this.selectCheck = []
      this.showPermDialog = false
    },
    async handelPermConfirm() {
      // 发请求
      await assignPerm({
        id: this.roleId,
        permIds: this.$refs.treeRef.getCheckedKeys()
      })
      this.$message.success('权限更改成功')
      this.getRoleList()
      this.closePerm()
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
